package com.chen.controller;

import com.chen.anno.Log;
import com.chen.pojo.Dept;
import com.chen.pojo.Result;
import com.chen.service.DeptService;
import com.chen.service.Impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    //private static final Logger log= LoggerFactory.getLogger(DeptController.class);
    @Autowired// 自动装配
    private DeptService deptService;
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        log.info("查询全部部门");
        List<Dept> deptList =deptService.findall();
        return Result.success(deptList);
    }
    /*原始方法：不推荐 繁琐
    删除部门的方法
     */
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idstr = request.getParameter("id");
//        int id =Integer.parseInt(idstr);
//        System.out.println("根据id删除了部门"+id);
//        return Result.success();
//    }
    /*
    方法二
     */
//    @DeleteMapping("depts")
//    public Result delete(@RequestParam(value = "id",required = false) Integer id){
//        System.out.println("根据id删除了部门"+id);
//        return Result.success();
//    }
    /*
    方法三
     */
    @Log
    @DeleteMapping
    public Result delete(Integer id){
        log.info("根据id删除了部门+{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    添加部门信息
     */
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门:+{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    /*
    查询回显部门信息
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询回显部门信息+{}",id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }
    /*
    修改部门信息
     */
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门信息+{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }
}
